﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DAO;
using HRProject.Code.Common;
using System.Collections.Generic;
using HRProject.Code.DTO;

namespace HRProject.Department
{
    public partial class RemoveRoleFromDepartment : System.Web.UI.Page
    {
        private Guid roleId;
        private Guid deptId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string id = Request.Params["roleId"];
                try
                {
                    roleId = new Guid(id);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                    roleId = Guid.Empty;
                }

                id = Request.Params["departmentId"];
                try
                {
                    deptId = new Guid(id);
                }
                catch (Exception exp)
                {
                    Console.Out.Write(exp.StackTrace);
                    deptId = Guid.Empty;
                }

                RoleDAO dao = new RoleDAO();
                BLRole filter = new BLRole();
                filter.Id = roleId;
                filter.Name = string.Empty;
                filter.Department = new BLDepartment();
                filter.Department.Id = deptId;
                List<BLRole> roles = dao.searchRolesInDepartment(filter);
                if (roles.Count > 0)
                {
                    hdfRoleId.Value = roleId.ToString();
                    hdfDepartmentId.Value = roles[0].Department.Id.ToString();
                    tbxName.Text = roles[0].Name;
                    tbxDepartment.Text = roles[0].Department.Name;
                }
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!BLAuthentication.IsRemoveAllowed)
            {
                lblError.Text = ContentProvider.GetMessage("NotAuthorized");
                return;
            }

            try
            {
                roleId = new Guid(hdfRoleId.Value);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                roleId = Guid.Empty;
            }

            try
            {
                deptId = new Guid(hdfDepartmentId.Value);
            }
            catch (Exception exp)
            {
                Console.Out.Write(exp.StackTrace);
                deptId = Guid.Empty;
            }

            BLRole role = new BLRole();
            role.Id = roleId;
            role.Department = new BLDepartment();
            role.Department.Id = deptId;
            role.Name = tbxName.Text;
            role.DataSignature = 0;

            RoleDAO dao = new RoleDAO();
            dao.deleteRoleFromDepartment(role);

            Util.CloseDialog(Response);
        }
    }
}
